/**
 * 参考自：https://github.com/Jasonwang911/vue_mockjs/blob/master/src/mock.js
 * Created by fanGe on 2019-02-21 09:44:57.
 */

// 获取 mock.Random 对象
// const Random = Mock.Random

import MockAdapter from 'axios-mock-adapter'
import http from '@/utils/http'
import HardWare from '@/mock/equipControl/hardwareManage.js'
import { getMenus } from '@/mock/menuMock'
import { getDictionary } from '@/mock/dictionaryMock'

let Mock = new MockAdapter(http)
export const MSG_NODATA = '没有匹配的模拟数据'

/**
 * @param {*} data 返回数据
 */

export let formatRes = function (data) {
  return {
    'code': '200',
    'msg': '执行成功',
    data: data
  }
}

/**
 * 格式化错误
 * @param code 错误代码
 * @param errmsg 错误信息
 * @returns {{errcode: *, errmsg: *}}
 */
//
export let formatError = function (code, errmsg, url, method) {
  if (url) {
    errmsg = url + ':' + errmsg
  }
  if (method) {
    errmsg = method + '@' + errmsg
  }
  return {
    'code': code || 200,
    'message': errmsg
  }
}

let retuData = {
  success: true
}
// 登录
Mock.onPost('cpe/login').reply(config => {
  return new Promise((resolve) => {
    if (config.data) {
      resolve([200, formatRes(retuData)])
    }
  })
})

// 获取导航菜单菜单
Mock.onGet('pf/sys/menu').reply(config => {
  return new Promise((resolve, reject) => {
    let menus = getMenus()
    if (menus) {
      resolve([200, formatRes(menus)])
    } else {
      reject(formatError(200, '未获取到菜单'))
    }
  })
})

// 获取字典
Mock.onGet('pf/sys/dict/dictAll').reply(config => {
  return new Promise((resolve, reject) => {
    let menus = getDictionary()
    if (menus) {
      resolve([200, formatRes(menus)])
    } else {
      reject(formatError(200, '未获取到菜单'))
    }
  })
})

let hardwareMock = new HardWare(Mock)
hardwareMock.mockApi()
